package com.systemCapacity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systemCapacity.common.dto.AttributeDto;
import com.systemCapacity.common.dto.BlueEquipmentDto;
import com.systemCapacity.common.dto.EquipmentDto;
import com.systemCapacity.common.dto.EquipmentTemplateDto;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.entity.Enum.PlatformType;
import com.systemCapacity.entity.Enum.TemplateType;
import com.systemCapacity.entity.Equipment;
import com.systemCapacity.entity.EquipmentTemplate;
import com.systemCapacity.service.AttributeService;
import com.systemCapacity.service.EquipmentService;
import com.systemCapacity.service.EquipmentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 任杰
 * @since 2023.6.15
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentTemplateService equipmentTemplateService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private AttributeService attributeService;

    /**
     * 查询特定平台的装备模板
     * @param type 模板类型
     * @param page 起始页码
     * @param size 分页长度
     * @return 操作结果
     */
    @GetMapping("/template/list")
    public Result getEquipmentTemplates(@RequestParam(value = "type") int type,
                                        @RequestParam(value = "belong") int belong,
                                        @RequestParam(value = "page", defaultValue = "0") int page,
                                        @RequestParam(value = "size", defaultValue = "20") int size) {
        if (page < 0) {
            return Result.fail("分页查询参数不能为负");
        } else if (size <= 0) {
            return Result.fail("分页数量必须大于0");
        } else if (type < 0 || type >= TemplateType.values().length) {
            return Result.fail("请选择正确的平台");
        }
        return Result.succ(equipmentTemplateService.getTemplates(type, belong, page, size));
    }

    /**
     * 保存作战装备模板
     * @param dto 作战装备模板dto
     * @return 操作结果
     */
    @PostMapping("/template/save")
    public Result saveEquipmentTemplate(@RequestBody @Validated EquipmentTemplateDto dto) {
        Assert.notNull(dto.getTemplateType(), "模板类型不能为空");
        if (dto.getTemplateType().getValue() < 0 || dto.getTemplateType().getValue() >= TemplateType.values().length) {
            return Result.fail("请选择正确的平台");
        }
        if (dto.getTemplateType().getValue().intValue() != TemplateType.PLATFORM.getValue().intValue()
                && dto.getPlatformType() != null) {
            return Result.fail("非平台设备不能选择平台类型");
        }
        HashMap<String, Object> attrNameMap = new HashMap<>();
        // 先校验添加属性中是否含有重复属性
        for (AttributeDto attribute : dto.getAttributes()) {
            if (attrNameMap.containsKey(attribute.getAttributeName())) {
                return Result.fail("属性名称不能重复");
            }
            attrNameMap.put(attribute.getAttributeName(), null);
        }
        equipmentTemplateService.saveEquipmentTemplate(dto);
        return Result.succ(null);
    }

    /**
     * 编辑装备模板
     * @param dto 装备模板
     * @return 操作结果
     */
    @PostMapping("/template/edit")
    public Result editEquipmentTemplate(@RequestBody @Validated EquipmentTemplateDto dto) {
        Assert.notNull(dto.getTemplateId(), "模板id不能为空");
        if (!equipmentTemplateService.templateExist(dto.getTemplateId())) {
            return Result.fail("装备模板不存在");
        }
        QueryWrapper<EquipmentTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("templateName", dto.getTemplateName());
        queryWrapper.eq("templateType", dto.getTemplateType());
        EquipmentTemplate template = equipmentTemplateService.getOne(queryWrapper);
        if (template != null && !template.getTemplateId().equals(dto.getTemplateId())) {
            return Result.fail("装备模板名称已存在");
        }
        equipmentTemplateService.editEquipmentTemplate(dto);
        return Result.succ(null);
    }

    /**
     * 保存装备
     * @param dto 装备dto
     * @return 保存结果
     */
    @PostMapping("/equipment/save")
    public Result saveEquipment(@RequestBody @Validated EquipmentDto dto) {
        Assert.notNull(dto.getTemplateId(), "请选择装备模板");
        if (!equipmentTemplateService.templateExist(dto.getTemplateId())) {
            return Result.fail("装备模板不存在");
        }
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipmentName", dto.getEquipmentName());
        queryWrapper.eq("pTemplateId", dto.getTemplateId());
        Equipment equipment = equipmentService.getOne(queryWrapper);
        if (equipment != null && !equipment.getEquipmentId().equals(dto.getEquipmentId())) {
            return Result.fail("装备名称已存在");
        }
        for (AttributeDto attribute : dto.getAttributes()) {
            if (attributeService.getParentAttribute(attribute, dto.getTemplateId()) == null) {
                return Result.fail(
                        String.format("装备属性%s不存在父属性，请确认装备模板中是否存在相关属性", attribute.getAttributeName())
                );
            }
        }
        equipmentService.saveEquipment(dto);
        return Result.succ(null);
    }

    /**
     * 编辑装备模板
     * @param dto 装备dto
     * @return 编辑结果
     */
    @PostMapping("/equipment/edit")
    public Result editEquipment(@RequestBody @Validated EquipmentDto dto) {
        Assert.notNull(dto.getEquipmentId(), "请选择需要编辑的装备");
        if (!equipmentService.equipmentExist(dto.getEquipmentId())) {
            return Result.fail("当前装备不存在");
        }
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipmentName", dto.getEquipmentName());
        queryWrapper.eq("pTemplateId", dto.getTemplateId());
        Equipment equipment = equipmentService.getOne(queryWrapper);
        if (equipment != null && !equipment.getEquipmentId().equals(dto.getEquipmentId())) {
            return Result.fail("装备名称已存在");
        }
        equipmentService.editEquipment(dto);
        return Result.succ(null);
    }

    /**
     * 编辑装备模板
     * @return 编辑结果
     */
    @PostMapping("/equipment/editBlue")
    public Result editBlueEquipment(@RequestBody List<BlueEquipmentDto> equipmentList) {
        if(equipmentList != null && !equipmentList.isEmpty()){
            Set<String> set = equipmentList.stream().map(x -> x.getEquipmentName()).collect(Collectors.toSet());
            if(set.size() != equipmentList.size()){
                return Result.fail("装备名称重复");
            }
        }
        equipmentService.editBlueEquipment(equipmentList);
        return Result.succ(null);
    }


    /**
     * 查看装备属性
     * @param equipmentId 装备id
     * @return 装备属性
     */
    @GetMapping("/equipment/attributes")
    public Result getEquipmentAttributes(@RequestParam(value = "equipmentId") String equipmentId) {
        if (!equipmentService.equipmentExist(equipmentId)) {
            return Result.fail("当前装备不存在");
        }
        return Result.succ(attributeService.getEquipmentAttributes(equipmentId));
    }

    /**
     * 查看蓝方装备属性
     * @param equipmentId 装备id
     * @return 装备属性
     */
    @GetMapping("/equipment/blueAttributes")
    public Result getEquipmentBlueAttributes(@RequestParam(value = "equipmentId") String equipmentId) {
        if (!equipmentService.equipmentExist(equipmentId)) {
            return Result.fail("当前装备不存在");
        }
        return Result.succ(attributeService.getEquipmentBlueAttributes(equipmentId));
    }

    /**
     * 查看蓝方平台装备参数
     * @return 装备参数
     */
    @GetMapping("/equipment/bluePlatformAttributes")
    public Result getEquipmentBluePlatformAttributes(String equipmentId, String deviceId) {
        if (!equipmentService.equipmentExist(equipmentId)) {
            return Result.fail("当前装备不存在");
        }
        return Result.succ(attributeService.getEquipmentBlueAttributesDesc(equipmentId,deviceId));
    }

    /**
     * 查看蓝方装备名
     * @param equipmentId 装备id
     * @return 查看蓝方装备名
     */
    @GetMapping("/equipment/blueEquipmentNameList")
    public Result getBlueEquipmentNameList(@RequestParam(value = "equipmentId") String equipmentId) {
        if (!equipmentService.equipmentExist(equipmentId)) {
            return Result.fail("当前装备不存在");
        }
        List<BlueEquipmentDto> equipmentBlueAttributeList = attributeService.getEquipmentBlueAttributes(equipmentId);
        List<String> list = equipmentBlueAttributeList.stream().map(x -> x.getEquipmentName()).collect(Collectors.toList());
        return Result.succ(equipmentBlueAttributeList);
    }

    /**
     * 查看蓝方数据链属性
     * @param equipmentId 装备id
     * @return 装备属性
     */
    @GetMapping("/equipment/blueDataLinkAttributes")
    public Result getEquipmentBlueDataLinkAttributes(@RequestParam(value = "equipmentId") String equipmentId) {
        if (!equipmentService.equipmentExist(equipmentId)) {
            return Result.fail("当前装备不存在");
        }
        return Result.succ(attributeService.getEquipmentBlueAttributes(equipmentId));
    }
    /**
     * 编辑蓝方数据链属性
     * @return 编辑结果
     */
    @PostMapping("/equipment/editDataLinkAttr")
    public Result editDataLinkAttr(@RequestBody List<AttributeDto> attributeList) {
        try {
            equipmentService.editDataLinkAttr(attributeList);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.succ(null);
    }
    /**
     * 查看装备模板属性
     * @param templateId 装备模板id
     * @return 装备模板属性
     */
    @GetMapping("/template/attributes")
    public Result getTemplateAttributes(@RequestParam(value = "templateId") String templateId) {
        if (!equipmentTemplateService.templateExist(templateId)) {
            return Result.fail("当前装备模型不存在");
        }
        return Result.succ(attributeService.getTemplateAttributes(templateId));
    }

    /**
     * 查看装备下拉菜单
     * @return 装备下拉菜单
     */
    @GetMapping("/menu")
    public Result getEquipmentMenu(Integer taskType) {
        return Result.succ(equipmentTemplateService.getTemplateMenu(taskType));
    }

    /**
     * 查看蓝方装备下拉菜单
     * @return 装备下拉菜单
     */
    @GetMapping("/blueMenu")
    public Result getBlueEquipmentMenu() {
        return Result.succ(equipmentTemplateService.getBlueTemplateMenu());
    }

    /**
     * 查看红方装备下拉菜单
     * @return 装备下拉菜单
     */
    @GetMapping("/redMenu")
    public Result getRedEquipmentMenu() {
        return Result.succ(equipmentTemplateService.getRedTemplateMenu());
    }

    /**
     * 删除装备
     */
    @GetMapping("/equipment/delete")
    public Result deleteEquipment(String equipmentId) {
        equipmentService.deleteEquipmentByEquipmentId(equipmentId);
        return Result.succ(null);
    }
}
